package service

import (
	"errors"
	"gitee.com/sunny-zhangqiang/zcache_new/config"
	"gitee.com/sunny-zhangqiang/zcache_new/logger"
)

var (
	accessLogger *logger.Logger
)

func AccessLogInit() error {
	if !config.ServerConf.EnableAccessLog {
		return nil
	}

	accessLogger = logger.NewLogger(false)

	scrollingMode := logger.ScrollingNone
	switch config.ServerConf.AccessLogScrollingMode {
	case "none":
		scrollingMode = logger.ScrollingNone
	case "minute":
		scrollingMode = logger.ScrollingMinMode
	case "hour":
		scrollingMode = logger.ScrollingHourMode
	case "day":
		scrollingMode = logger.ScrollingDayMode
	default:
		return errors.New("invalid access log scrolling mode")
	}

	fileOutput, err := logger.NewFileOutputAsync(config.ServerConf.AccessLogPath,
		scrollingMode, true, 5, 0, 4)
	if err != nil {
		return err
	}

	accessLogger.SetLevel(logger.DebugLevel)
	accessLogger.AddOutput(fileOutput)
	return nil
}

func printAccessLog(r *request) {
	if accessLogger == nil {
		return
	}
	accessLogger.Error("")
}
